class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

class LxLinkedList {
    constructor() {
        this.size = 0;
        this.head = null;
    }

    append(data) {
        var node = new Node(data);
        if (this.size == 0) {
            this.head = node;
        } else {
            var current = this.head;
            while (current.next) {
                current = current.next;
            }
            current.next = node;
        }
        this.size++;
    }

    toString(tag = " ") {
        if (this.size === 0) {
            return "";
        } else {
            var current = this.head;
            var str = "";
            while (current) {
                str += current.data + tag;
                current = current.next;
            }
            return str;
        }
    }

    insert(index, data) {
        var node = new Node(data);
        if (this.size < index) return false;
        if (index === 0) {
            node.next = this.head;
            this.head = node;
        } else {
            let current = this.head;
            for (var i = 0; i < index - 1; i++) {
                current = current.next;
            }
            node.next = current.next;
            current.next = node;
        }
        this.size++;
    }

    update(index, data) {
        var node = new Node(data);
        if (this.size <= index) return false;
        if (index === 0) {
            node.next = this.head.next;
            this.head = node;
        } else {
            let current = this.head;
            for (var i = 0; i < index - 1; i++) {
                current = current.next;
            }
            node.next = current.next.next;
            current.next = node;
        }
    }

    get(index) {
        if (this.size <= index) return undefined;
        let current = this.head;
        for (var i = 0; i < index; i++) {
            current = current.next;
        }
        return current.data;
    }

    indexOf(data) {
        let current = this.head;
        let index = 0;
        let isFind = false;
        while (!isFind && index < this.size - 1) {
            if (data == current.data) {
                isFind = true;
            } else {
                current = current.next;
                index++;
            }
        }
        if (!isFind) return -1;
        return index;
    }

    removeAt(index) {
        if (!this.size) return false;
        if (index === 0) {
            this.head = this.head.next;
        } else {
            let previous = this.head;
            let count = 0;
            let current = this.head;
            while (count < index - 1) {
                previous = current.next;
                count++;
            }
            previous.next = previous.next.next;
        }
        this.size--;
    }

    remove() {
        this.head = null;
        this.size = 0;
    }
}

var linkedList = new LxLinkedList();
linkedList.append(1);
linkedList.append(2);
// linkedList.append(3);
// linkedList.insert(1, 4);
// linkedList.update(2, 11);
// console.log(linkedList.toString());
// console.log(linkedList.get(1));
// console.log(linkedList.indexOf(111));
linkedList.remove();
console.log(linkedList);
